package com.zz.examine.case2;

import com.sun.javafx.image.IntPixelGetter;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String args[]){
        //1.把数据拿到程序中
        String userStrs = "1001:张三:男:1998-02-23#1002:李四:女:1849-12-12#1003:王五:男:2000-12-12#1003:张三:女:2010-12-12";
        //2.定义一个用户类
        //3.提前定义一个List<User>集合用于存储用户集合
        List<User> users = new ArrayList <>();
        //4.把全部用户数据，用#分割
        String[] userStrArray = userStrs.split("#");
        for (String userStr : userStrArray) {
            //userStr = "1001:张三:男:1998-02-23"
            //5.创建一个用户对象，封装这个用户数据
            User user = new User();
            String[] userInfo = userStr.split(":");
            user.setId(Long.valueOf(userInfo[0]));
            user.setName(userInfo[1]);
            user.setGender(userInfo[2]);
            user.setBirthday(LocalDate.parse(userInfo[3]));
            //6.把这个用户存入到集合总
            users.add(user);
        }
        System.out.println(users);

        System.out.println("============业务二===========");
        //7.遍历List集合中的用户对象，统计相同名称出现的次数
        Map<String, Integer> result = new HashMap <>();
        for (User user : users) {
            String name = user.getName();
            if(result.containsKey(name)){
                result.put(name, result.get(name) + 1);
            }else{
                result.put(name, 1);
            }
        }
        //8.遍历Map集合
        result.forEach((k,v)->{
            System.out.println(k + ":" + v + "次");
        });
    }
}
